package net.ocps.tchs.scarymazegame;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import net.ocps.tchs.scarymazegame.splash.splash;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Pixmap.Format;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;  



public class scarymazegame extends Game {
	private OrthographicCamera camera;
	//	private SpriteBatch batch;
	private SpriteBatch batch;

	private Texture texture;

	private Sprite sprite;

	private BitmapFont font;

	private ShapeRenderer shapeRenderer;
	private Pixmap pixels;
	private BufferedImage img;
	private int ZOOM_LENGTH = 300;
	private int ZoomScare = ZOOM_LENGTH + 100;
	private int ZoomSpeed = 9;
	private Sound sound;
	private int CircleX =  0;
	private int CircleY =  0;
	private boolean[][] map = new boolean [512][512];
	@Override

	public void create() {	

		setScreen(new splash());
		

		camera = new OrthographicCamera(512,512);

		batch = new SpriteBatch();

		texture = new Texture(Gdx.files.internal("data/mazeEdited.png"));
		//	Texture.setEnforcePotImages(false);
		texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		//circleImg = new Texture(Gdx.files.internal("data/circle.jpg"));

		sound = Gdx.audio.newSound(Gdx.files.internal("data/scream.mp3"));

		TextureRegion region = new TextureRegion(texture, 0, 0, 512, 512);

		sprite = new Sprite(region);
		sprite.setBounds(0,0,512,512);
		sprite.setOrigin(0, 0);
		//	sprite.setPosition(-sprite.getWidth(), -sprite.getHeight());

		font = new BitmapFont();
		pixels = new Pixmap(Gdx.files.internal("data/words.png"));
		int val = pixels.getPixel(256, 256);
		System.out.println(val);
		
		for(int i = 0; i < 512; i++) {
			for(int x = 0; x < 512; x++) {
				int lulz = pixels.getPixel(x, i);
				if(lulz != -1) map[x][i] = true;
				else map [x][i] = false;
				System.out.print(map[x][i]);
			}
			System.out.println();
		}
	}

	@Override
	public void dispose() {
		batch.dispose();
		texture.dispose();
		super.dispose();
	}

	@Override
	public void render() {		
		Gdx.gl.glClearColor(1, 1, 1, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);


		if(Gdx.input.isKeyPressed(Keys.DPAD_LEFT)) CircleX-=1;
		if(Gdx.input.isKeyPressed(Keys.DPAD_RIGHT)) CircleX+=1;
		if(Gdx.input.isKeyPressed(Keys.DPAD_UP)) CircleY+=1;
		if(Gdx.input.isKeyPressed(Keys.DPAD_DOWN)) CircleY-=1;

		int Perim = -9;
		if(CircleX > -248 && CircleX < 248 && CircleY > -248 && CircleY < 248)		{
			//	System.out.println(img.getRGB(Gdx.input.getX(), Gdx.input.getY()));



			//int Bound = 0;
			//int DiagBound = 0;
			CircleX += 256;
			CircleY += 256;

			CircleX -= 8;
			CircleY += 16;

			//	Perim = pixels.getPixel(CircleX, CircleY);
			System.out.println(map[CircleX][CircleY]);
			//	Perim = img.getRGB(CircleX, CircleY);
			//	Perim *= 9;
			/*
			 
			Perim += img.getRGB(CircleX+Bound, CircleY);
			Perim += img.getRGB(CircleX-Bound, CircleY);

			Perim += img.getRGB(CircleX, CircleY+Bound);
			Perim += img.getRGB(CircleX, CircleY-Bound);

			Perim += img.getRGB(CircleX+DiagBound, CircleY+DiagBound);
			Perim += img.getRGB(CircleX-DiagBound, CircleY+DiagBound);

			Perim += img.getRGB(CircleX+DiagBound, CircleY-DiagBound);
			Perim += img.getRGB(CircleX-DiagBound, CircleY+DiagBound);
			 */
			
			if(map[CircleX][CircleY]) {
				Perim = 0;
				if(ZoomScare == -1)		ZoomScare = 0;
			}
			
			CircleX -= 256;
			CircleY -= 256;

			CircleX += 8;
			CircleY -= 16;
		}

		camera.update();

		batch.setProjectionMatrix(camera.combined);

		if(ZoomScare >= ZOOM_LENGTH) {
			batch.begin();

			texture = new Texture(Gdx.files.internal("data/photo.png"));
			//	Texture.setEnforcePotImages(false);
			texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
			//circleImg = new Texture(Gdx.files.internal("data/circle.jpg"));

			TextureRegion region = new TextureRegion(texture, 0, 0, 512, 512);

			sprite = new Sprite(region);
			sprite.setBounds(0,0,512,512);
			sprite.setOrigin(0, 0);



			sprite.setPosition(0,0);
			
			batch.draw(region, -256, 256, 0, 0, 512, 512,1, 1, 45, false) ;
			
		//	sprite.draw(batch);
			font.setColor(0f, 0f, 0f, 1.0f);

			font.draw(batch, "Center cursor", -45, 20);

			batch.end();

			//		CircleX = ((Gdx.input.getX()-256-8));
			//		CircleY =  ((-Gdx.input.getY()+256-8));
			if((CircleX > -30 && CircleX < 30 && CircleY > -30 && CircleY < 30))		{
				ZoomScare = -1;
			}
		}

		if(Perim == -9 && ZoomScare == -1) {
			batch.begin();
			texture = new Texture(Gdx.files.internal("data/mazeEdited.png"));
			//	Texture.setEnforcePotImages(false);
			texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
			//circleImg = new Texture(Gdx.files.internal("data/circle.jpg"));

			TextureRegion region = new TextureRegion(texture, 0, 0, 512, 512);

			sprite = new Sprite(region);
			sprite.setBounds(0,0,512,512);
			sprite.setOrigin(0, 0);

			sprite.setPosition(-256,-256);
			sprite.draw(batch);
			texture = new Texture(Gdx.files.internal("data/circleblack.png"));
			texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
			//TextureRegion region = new TextureRegion(texture, 0, 0, 64, 64);
			sprite = new Sprite(texture);

			sprite.setBounds(0,0,16,16);
			sprite.setOrigin(-50, 50);

			sprite.setPosition((int)CircleX,(int) CircleY);

			sprite.draw(batch);
			batch.end();

		}
		else if (ZoomScare == -1){
			ZoomScare = 0;
		}


		if(ZoomScare < ZOOM_LENGTH && ZoomScare != -1) {
			System.out.println(ZoomScare);
			if(ZoomScare == 0) sound.play(1f);
			batch.begin();
			texture = new Texture(Gdx.files.internal("data/photo.jpg"));
			//	Texture.setEnforcePotImages(false);
			texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
			//circleImg = new Texture(Gdx.files.internal("data/circle.jpg"));

			int ZoomPos = 512 - ZoomScare;



			TextureRegion region = new TextureRegion(texture, ZoomScare/2, ZoomScare/2, ZoomPos, ZoomPos);



			sprite = new Sprite(region);
			sprite.setBounds(0,0,512,512);
			sprite.setOrigin(0, 0);


			sprite.setPosition(-256,-256);
			sprite.draw(batch);
			ZoomScare+=ZoomSpeed;
			batch.end();
		}


		super.render();
	}

	@Override
	public void resize(int width, int height) {
	super.resize(width, height);
	}
	

	@Override
	public void pause() {
		super.pause();
	}

	@Override
	public void resume() {
		super.resume();
	}
}
